package com.enhinck.dbutilweb.controller;


import com.enhinck.dbutilweb.PropertiesListenerConfig;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


/**
 * 验证码配置
 * @author huenb
 */
@Component
@Data
public class KaptchaConfig {

    private DefaultKaptcha defaultKaptcha;

    public static final String KAPTCHA_CONFIG_PREFIX = "kaptcha.";

    private void createKaptcha() {
        defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        Map<String, String> map = PropertiesListenerConfig.getAllProperty();

        map.forEach((key,value) -> {
            if (key.startsWith(KAPTCHA_CONFIG_PREFIX)) {
                properties.setProperty(key, value);
            }
        });
        com.google.code.kaptcha.util.Config config = new com.google.code.kaptcha.util.Config(properties);
        defaultKaptcha.setConfig(config);
    }


    @PostConstruct
    public void init() {
        createKaptcha();
    }
}